package work02.w5.control;

import java.util.Calendar;
import java.util.Date;

import work02.w5.model.DateWithLunar;

public class MyCalendar {
	Calendar cal = Calendar.getInstance();
	Lunar lu = new Lunar();

	/**
	 * 得到一个月的所有日期模型
	 * 
	 * @param year
	 * @param month
	 *            阳历的月份，请按照Calendar中的常量来判断 0-11
	 * @return
	 */
	public DateWithLunar[] getAMonthDate(int year, int month) {
	
		cal.set(year, month, 1);
		/*
		 * 得到最后一天
		 */
		int month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		DateWithLunar[] dates = new DateWithLunar[month_day_score];
		for (int i = 0; i < dates.length; i++) {
			dates[i] = geADate(year, month, i+1);
		}
		return dates;
	}

	/**
	 * @param year
	 * @param month
	 *            阳历的月份，请按照Calendar中的常量来判断 0-11
	 * @return
	 */
	public DateWithLunar geADate(int year, int month, int day) {
		cal.set(year, month, day);
		lu.setDay(cal);
		return new DateWithLunar(lu.toYear(), lu.toMonth(), lu.toDay(),
				cal.get(Calendar.DATE), cal.get(Calendar.DAY_OF_WEEK), month,
				year, cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
	}

	/**
	 * 得到今天
	 * 
	 * @return
	 */
	public DateWithLunar getTodayDate() {
		cal.setTime(new Date());
		return geADate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DATE));
	}

	public boolean isSameDay(DateWithLunar d1, DateWithLunar d2) {
		DateWithLunar today = d2;
		if (d1.getTimeOfMonth() == today.getTimeOfMonth()
				&& d1.getTimeOfYear() == today.getTimeOfYear()
				&& d1.getTimeOfDay() == today.getTimeOfDay())
			return true;
		return false;
	}

	/**
	 * 比较是否同月
	 * 
	 * @param d1
	 * @param d2
	 * @return 如果d1和d2同月，返回0 如果d1在d2之前，返回负数-1 如果d1在d2之后，返回正数1
	 */
	public int comparedMonth(DateWithLunar d1, DateWithLunar d2) {
		if (d1.getTimeOfYear() > d2.getTimeOfYear())
			return 1;
		if (d1.getTimeOfYear() < d2.getTimeOfYear())
			return -1;
		if (d1.getTimeOfYear() == d2.getTimeOfYear()) {
			if (d1.getTimeOfMonth() > d2.getTimeOfMonth())
				return 1;
			if (d1.getTimeOfMonth() < d2.getTimeOfMonth())
				return -1;
			if (d1.getTimeOfMonth() == d2.getTimeOfMonth())
				return 0;
		}
		return 0;
	}

	/**
	 * 是否为工作日
	 * 
	 * @param d
	 * @return
	 */
	public boolean isWeekDay(DateWithLunar d) {
		switch (d.getTimeOfWeek()) {
		case Calendar.SUNDAY:
			return false;
		case Calendar.SATURDAY:
			return false;
		default:
			return true;

		}
	}

	/**
	 * 得到这天的开始的时间
	 * 
	 * @param d
	 * @return
	 */
	public Date getTheDayStart(DateWithLunar d) {
		cal.set(d.getTimeOfYear(), d.getTimeOfMonth(), d.getTimeOfDay());
		return cal.getTime();
	}

	/**
	 * 得到这天的结束的时间即下一天的开始
	 * 
	 * @param d
	 * @return
	 */
	public Date getTheDayEnd(DateWithLunar d) {
		/*
		 * 一天的开始加上24小时
		 */
		return new Date(getTheDayStart(d).getTime() + 24 * 60 * 60 * 1000);
	}
	//
}
